<?php

namespace app\api\controller;

use app\api\model\Classes;
use app\api\model\Department;
use app\api\model\Major;
use app\api\model\User;
use think\Controller;

class Register extends Controller
{

    /**
     * 用户注册
     * @return \think\response\Json
     */
    public function index()
    {
        // 通过框架定义的助手函数判断是否post请求，如果不是的话返回无效请求
        if (!request()->isPost()) {
            return fail(INVALID_REQUEST, '无效的请求');
        }

        $params = input("post.");
        $validate = new \app\api\validate\Register();
        if (!$validate->check($params)) {
            return fail(INVALID_PARAMS, $validate->getError());
        }

        if ($params['password'] !== $params['confirm_password']) {
           return fail(INVALID_PARAMS, '两次密码不一致');
        }
        $department = Department::queryById($params['department_id']);
        if (!$department) {
            return fail(INVALID_PARAMS, '该学院不存在');
        }
        $major = Major::queryById($params['major_id']);
        if (!$major) {
            return fail(INVALID_PARAMS, '该专业不存在');
        }
        $class = Classes::queryByClassId($params['class_id']);
        if (!$class) {
            return fail(INVALID_PARAMS, '该班级不存在');
        }
        $params['phone'] = input('?post.phone') ? input('post.phone') : '';
        $params['username'] = input('?post.username') ? input('post.username') : '';
        
        $result = User::createUser($params);
        if (!$result) {
            return fail(SAVE_DATA_FAIL, '注册失败，请重试');
        }
        return success('注册成功');
    }

    /**
     * 获取学院和相应专业列表
     * @return \think\response\Json
     */
    public function getDepartmentList()
    {
        $result = Department::queryDepartmentAndMajor();
        return success($result);
    }

    /**
     * 根据专业id返回相应的班级
     * @return \think\response\Json
     */
    public function getClassList()
    {
        // 获取前端传过来的专业id，判断是否有传并且不为空
        $majorId = input("?get.major_id") ? input("get.major_id") : 0;
        if (!$majorId) {
            return fail(INVALID_PARAMS, '请选择专业id');
        }
        $result = Classes::queryListByMajorId($majorId);
        return success($result);
    }

}